package cn.iocoder.an.module.mold.controller.admin.account;

import cn.iocoder.an.module.mold.controller.admin.category.vo.CategoryImportExcelVO;
import cn.iocoder.an.module.mold.dal.dataobject.drawingno.DrawingNoDO;
import cn.iocoder.an.module.mold.utils.ImportRespVO;
import com.fhs.core.trans.anno.TransMethodResult;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.account.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.account.AccountDO;
import cn.iocoder.an.module.mold.service.account.AccountService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 模具台账")
@RestController
@RequestMapping("/mold/account")
@Validated
public class AccountController {
    @Resource
    private AccountService accountService;

    @PostMapping("/create")
    @Operation(summary = "创建模具台账")
    @PreAuthorize("@ss.hasPermission('mold:account:create')")
    public CommonResult<List<Long>> createAccount(@Valid @RequestBody AccountSaveReqVO createReqVO) {
        return success(accountService.createAccounts(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具台账")
    @PreAuthorize("@ss.hasPermission('mold:account:update')")
    public CommonResult<Boolean> updateAccount(@Valid @RequestBody AccountSaveReqVO updateReqVO) {
        accountService.updateAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具台账")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:account:delete')")
    public CommonResult<Boolean> deleteAccount(@RequestParam("id") Long id) {
        accountService.deleteAccount(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具台账")
                @PreAuthorize("@ss.hasPermission('mold:account:delete')")
    public CommonResult<Boolean> deleteAccountList(@RequestParam("ids") List<Long> ids) {
        accountService.deleteAccountListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具台账")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:account:query')")
    @TransMethodResult
    public CommonResult<AccountRespVO> getAccount(@RequestParam("id") Long id) {
        AccountDO account = accountService.getAccount(id);
        return success(BeanUtils.toBean(account, AccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具台账分页")
    @PreAuthorize("@ss.hasPermission('mold:account:query')")
    @TransMethodResult
    public CommonResult<PageResult<AccountRespVO>> getAccountPage(@Valid AccountPageReqVO pageReqVO) {
        PageResult<AccountDO> pageResult = accountService.getAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具台账 Excel")
    @PreAuthorize("@ss.hasPermission('mold:account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAccountExcel(@Valid AccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AccountDO> list = accountService.getAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具台账.xls", "数据", AccountRespVO.class,
                        BeanUtils.toBean(list, AccountRespVO.class));
    }


    @GetMapping("/all")
    @Operation(summary = "获得所有模具列表")
    public CommonResult<List<AccountDO>> getDrawingNoAll() {
        return success(accountService.getAccountAll());
    }




    @GetMapping("/get-import-template")
    @Operation(summary = "获得模具台账导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<AccountImportExcelVO> list = Arrays.asList(
                AccountImportExcelVO.builder()
                        .name("测试")
                        .code("muju")
                        .drawingNo("MH-01")
                        .customer("小米")
                        .categoryCode("WG")
                        .deptName("部门")
                        .devCode("MH-01")
                        .totalCavity(10)
                        .availableCavity(1)
                        .totalUsageCount(100)
                        .usedCount(1)
                        .warningCount(90)
                        .delayCount(1)
                        .warehouseCode("123")
                        .storageCode("123")
                        .oprationStep(1)
                        .quantity(1)
                        .upkeepCode("1")
                        .lastUpkeepTime(LocalDateTime.now())
                        .nextUpkeepTime(LocalDateTime.now())
                        .upkeeperName("张三")
                        .recheckCode("1")
                        .lastRecheckTime(LocalDateTime.now())
                        .nextRecheckTime(LocalDateTime.now())
                        .recheckerName("张三")
                        .inventoryStatus(1)
                        .usageStatus(1)
                        .repairStatus(1)
                        .upkeepStatus(1)
                        .recheckStatus(1)
                        .description("描述内容")
                        .build()
        );
        // 输出
        ExcelUtils.write(response, "模具台账导入模板.xlsx", "列表", AccountImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入图号")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<AccountImportExcelVO> list = ExcelUtils.read(file, AccountImportExcelVO.class);
        return success(accountService.importAccountList(list, updateSupport));
    }

}